# To change this template, choose Tools | Templates
# and open the template in the editor.

from game.gui.button import RadialButton
import pygame
from pygame.locals import *
from game.gui.widget import *

class GameArea(Widget):
    def __init__(self, data, **args):
        Widget.__init__(self, **args)

        self.data = data

        #this is the number of sprites that a single level fills
        self.game_pitch = (60, 32)

        #this is the number of sprites viewable on a single level
        self.view_pitch = (24, 12)

        #this is the area in which the main character will always be
        #in within the view.
        self.view_bound = Rect(6, 4, 12, 4)
        
        #this is the current view offset
        self.view_offset = [0, 0]

        self.view_rect = Rect(0, 0,
            self.view_pitch[0] * data.sprite_pitch[0],
            self.view_pitch[1] * data.sprite_pitch[1])

        self.game_rect = Rect(0, 0,
            self.game_pitch[0] * data.sprite_pitch[0],
            self.game_pitch[1] * data.sprite_pitch[1])


    def receive(self, dat, evt):
        if self.enable:
            dat = self.data
            if evt.type == MOUSEBUTTONDOWN:
                if evt.button == 1:
                    mr_x, mr_y = evt.pos
                    bx, by = self.view_rect.size
                    if mr_x < bx and mr_y < by:
                        m_x, m_y =  (mr_x // dat.sprite_pitch[0]) + self.view_offset[0], (mr_y // dat.sprite_pitch[1]) + self.view_offset[1]
                        if not (m_x == dat.main_char.x and m_y == dat.main_char.y):
                            dat.main_char.target = [m_x, m_y]
                            dat.state_mgr.push_state("moving")
                        return 1
                if evt.button == 3:
                    mr_x, mr_y = evt.pos
                    bx, by = self.view_rect.size
                    if mr_x < bx and mr_y < by:
                        mn_x, mn_y =  (mr_x // dat.sprite_pitch[0]), (mr_y // dat.sprite_pitch[1])
                        m_x, m_y = mn_x + self.view_offset[0], mn_y +  + self.view_offset[1]
                        dd = dat.level.tiles[m_x][m_y].doodad
                        if dd is not None:
                            if dd.name == 'door':
                                menu = dat.gui.create_radial_menu((mr_x, mr_y), (120, 120))
                                for i in xrange(8):
                                    idle = dat.res_mgr.load_subimage(dat.res_mgr.load_image('gui_16.png'), 0, 0, 16, 16)
                                    hover = dat.res_mgr.load_subimage(dat.res_mgr.load_image('gui_16.png'), 0, 16, 16, 16)
                                    mi = RadialButton(modal=1, icon_idle=idle, icon_hover=hover)
                                    mi.action_perform = 'close'

                                    mi.change_state('idle')
                                    mi.rect[2], mi.rect[3] = 16, 16
                                    menu.add_item(mi, i)
                                    
                                dat.gui.root.add_child(menu, -1)
                                return 1

            elif evt.type == MOUSEMOTION:
                mr_x, mr_y = evt.pos
                bx, by = self.view_rect.size
                if mr_x < bx and mr_y < by:
                    #print 'summertears'
                    m_x, m_y =  mr_x - (mr_x % dat.sprite_pitch[0]), mr_y - (mr_y % dat.sprite_pitch[1])
                    dat.hilite.rect = m_x, m_y, dat.sprite_pitch[0], dat.sprite_pitch[1]
                    dat.hilite.valid = 1
                else:
                    #print 'machin'
                    dat.hilite.valid = 0

        return 0

    def draw(self, canvas, area=None):
        #if area is None:

        dat = self.data
        vr = self.view_rect
        vo = self.view_offset
        vp = self.view_pitch
        sp = dat.sprite_pitch

        if self.dirty:
            self.canvas = pygame.Surface((vr[2], vr[3])).convert()
            self.canvas.blit(dat.level.background, (0, 0), vr)
#
#            dat.item_sprites.update()
#            dat.item_sprites.draw(self.canvas, (vr[0], vr[1]))
#
#            dat.char_sprites.update()
#            dat.char_sprites.draw(self.canvas, (vr[0], vr[1]))

            x = 0
            for tx in range(vo[0], vp[0] + vo[0]):
                y = 0
                for ty in range(vo[1], vp[1] + vo[1]):
                    #first do visibility
                    vis = dat.tile_invis
                    t = dat.level.tiles[tx][ty]

                    if t.visible == 'none':
                        vis = dat.tile_invis

                    elif t.visible == 'fog':
                        #draw the last known stuff here
                        vis = dat.tile_fogvis
                    #don't bother drawing any doodads, units or items if the tile is not visible at all
                    else:
                        vis = dat.tile_vis
                        #draw actual items and doodads
                        if t.doodad is not None:
                            #self.canvas.blit(t.doodad.image, (x * sp[0], y * sp[1]))
                            pass
                        elif len(t.items) > 0:
                            self.canvas.blit(t.items[0].image, (x * sp[0], y * sp[1]))
                        draw_plus = (t.doodad is not None and len(t.items) > 0) or len(t.items) > 1
                        if draw_plus:
                            #draw a plus to show there is more than is drawn
                            pass

                        #draw occupant
                        if t.occupant is not None:
                            self.canvas.blit(t.occupant.image, (x * sp[0], y * sp[1]))
                            #pass

                    self.canvas.blit(vis, (x * sp[0], y * sp[1]))

                    y = y + 1
                #end for
                x = x + 1
            #end for
            canvas.blit(self.canvas, (0,0))
            self.dirty = 0
        else:
            canvas.blit(self.canvas, (0,0))

    def adjust_view(self):
        #This may not always be the main_char pos, but for now it is
        p = self.data.main_char.pos()
        

        if p[0] < (self.view_bound.left + self.view_offset[0]):
            self.view_offset[0] = max(p[0] - self.view_bound.left, 0)
            self.view_rect[0] = self.view_offset[0] * self.data.sprite_pitch[0]
        elif p[0] > (self.view_bound.right + self.view_offset[0]):
            self.view_offset[0] = min(p[0] - self.view_bound.right, self.game_pitch[0] - self.view_pitch[0])
            self.view_rect[0] = self.view_offset[0] * self.data.sprite_pitch[0]

        if p[1] < (self.view_bound.top + self.view_offset[1]):
            self.view_offset[1] = max(p[1] - self.view_bound.top, 0)
            self.view_rect[1] = self.view_offset[1] * self.data.sprite_pitch[1]
        elif p[1] > (self.view_bound.bottom + self.view_offset[1]):
            self.view_offset[1] = min(p[1] - self.view_bound.bottom, self.game_pitch[1] - self.view_pitch[1])
            self.view_rect[1] = self.view_offset[1] * self.data.sprite_pitch[1]
